package org.opendev.app.navybattle.domain;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.opendev.app.navybattle.domain.Coordinate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect CoordinateDataOnDemand_Roo_DataOnDemand {
    
    declare @type: CoordinateDataOnDemand: @Component;
    
    private Random CoordinateDataOnDemand.rnd = new SecureRandom();
    
    private List<Coordinate> CoordinateDataOnDemand.data;
    
    public Coordinate CoordinateDataOnDemand.getNewTransientCoordinate(int index) {
        org.opendev.app.navybattle.domain.Coordinate obj = new org.opendev.app.navybattle.domain.Coordinate();
        obj.setLatitude(null);
        obj.setLongitude(null);
        return obj;
    }
    
    public Coordinate CoordinateDataOnDemand.getSpecificCoordinate(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Coordinate obj = data.get(index);
        return Coordinate.findCoordinate(obj.getId());
    }
    
    public Coordinate CoordinateDataOnDemand.getRandomCoordinate() {
        init();
        Coordinate obj = data.get(rnd.nextInt(data.size()));
        return Coordinate.findCoordinate(obj.getId());
    }
    
    public boolean CoordinateDataOnDemand.modifyCoordinate(Coordinate obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void CoordinateDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = org.opendev.app.navybattle.domain.Coordinate.findCoordinateEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Coordinate' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<org.opendev.app.navybattle.domain.Coordinate>();
        for (int i = 0; i < 10; i++) {
            org.opendev.app.navybattle.domain.Coordinate obj = getNewTransientCoordinate(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
